<?php
App::uses('AppController', 'Controller');
/**
 * Images Controller
 *
 * @property Image $Image
 */
class ImagesController extends AppController {
	
	
	public function makePreviewImage($imageId) {
		if (!empty($imageId)) {
			$imageName = $this->Image->getNameById($imageId);
			$itemId = $this->Image->getItemById($imageId);
			$item = array();
			$item['Item']['id'] = $itemId;
			$item['Item']['photo'] = $imageName;
			$this->Image->Item->save($item, false);
			$this->Session->setFlash(__('Success'));
		}
		$this->redirect('index');
	}
	
	

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Image->recursive = 0;
		$this->set('images', $this->paginate());
        $this->pageTitle = '- Images';


        //$this->set('id', $this->Image->find('all', array('conditions' => $this->Image->data['Image']['item_id'])));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Image->exists($id)) {
			throw new NotFoundException(__('Invalid image'));
		}
		$options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
		$this->set('image', $this->Image->find('first', $options));
		
		$itemId = $this->Image->item_id;
		$item = $this->Image->Item->read(null, $itemId);
		$this->set('items', $item);
	}

/**
 * add method
 *
 * @return void
 */
public function add() {
        if ($this->request->is('post') || $this->request->is('put')) {
                $this->Image->set($this->data);
                if (is_uploaded_file($this->request->data['Image']['name']['tmp_name']) && !empty($this->request->data['Image']['name']['tmp_name'])) {
                        $path_info = pathinfo($this->request->data['Image']['name']['name']);
                        chmod ($this->request->data['Image']['name']['tmp_name'], 0644);
                        $photo = time() . mt_rand() . "." . $path_info['extension'];
                        $fullpath = WWW_ROOT."img".DS.IMG_DIR;
                        $res1 = is_dir($fullpath);
                        if ($res1 != 1) {
                                $res2 = mkdir($fullpath, 0777, true);
                        }
                        move_uploaded_file($this->request->data['Image']['name']['tmp_name'], $fullpath.DS.$photo);
                        $this->request->data['Image']['name'] = $photo;
                } else {
                        unset($this->request->data['Photo']['name']);
                }
                $this->Image->save($this->request->data);
                $this->Session->setFlash(__('upload successful'));
                $this->redirect('index');
        }
        $items = $this->Image->Item->find('list');
        $this->set(compact('items'));
}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		
		if (!$this->Image->exists($id)) {
			throw new NotFoundException(__('Invalid image'));
		}
		
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->request->is('post') || $this->request->is('put')) {
				if ($this->Image->save($this->request->data)) {
					$this->Session->setFlash(__('The image has been saved'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The image could not be saved. Please, try again.'));
				}
			} else {
				$options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
				$this->request->data = $this->Image->find('first', $options);
			}
			$items = $this->Image->Item->find('list');
			$this->set(compact('items'));
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
            
		$this->Image->id = $id;
		if (!$this->Image->exists()) {
			throw new NotFoundException(__('Invalid image'));
		}
		$imageName = $this->Image->getNameById($id);
		$this->request->onlyAllow('post', 'delete');
		if ($this->Image->delete()) {
                        $fullpath = WWW_ROOT."img".DS.IMG_DIR;
                        unlink($fullpath.DS.$imageName);
			$this->Session->setFlash(__('Image deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Image was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
        
        //----------------------------------------------------------------------
        
        public function beforeFilter() {
            parent::beforeFilter();
                    
                    $this->Auth->autoRedirect = false;
            
                    $this->Auth->allow('view');
        }
}
